﻿using Learun.Application.Base.SystemModule;
using Learun.Application.Organization;
using Learun.Util;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using static Learun.Application.TwoDevelopment.ZZDT_EC.ProjectSugar;
using static Learun.Util.SqlSugar.SqlSugarHelper;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-01-30 15:57
    /// 描 述：图纸文件
    /// </summary>
    public class ec_drawing_fileBLL : ec_drawing_fileIBLL
    {
        private ec_drawing_fileService ec_drawing_fileService = new ec_drawing_fileService();
        private ec_enginedata_pixelService ec_enginedata_pixelService = new ec_enginedata_pixelService();
        private ec_enginedataService ec_enginedataService = new ec_enginedataService();
        private ec_objecttypeService ec_objecttypeService = new ec_objecttypeService();
        #region 获取数据

        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_drawing_fileEntity> GetList(string queryJson, string engineDataID = "", Pagination pagination = null)
        {
            try
            {
                var queryParam = queryJson.ToJObject();
                var ProjectId = queryParam["ProjectId"]?.ToString();
                var DrawingCatalogueID = queryParam["DrawingCatalogueID"]?.ToString();
                var StrDrawingCatalogueIDs = queryParam["StrDrawingCatalogueIDs"]?.ToString();
                var DrawingFileNameFull = queryParam["DrawingFileNameFull"]?.ToString();
                var DrawingFileIDUnequal = queryParam["DrawingFileIDUnequal"]?.ToString();
                var IsDelete = queryParam["IsDelete"]?.ToString();
                var DrawingFileName = queryParam["DrawingFileName"]?.ToString();

                var tableName = TableName<ec_drawing_fileEntity>(ProjectId);
                var pixelTableName = TableName<ec_enginedata_pixelEntity>(ProjectId);
                var pixels = new List<ec_enginedata_pixelEntity>();
                var fileIds = new List<string>();
                if (!string.IsNullOrEmpty(engineDataID))
                {
                    pixels = Db.Queryable<ec_enginedata_pixelEntity>().AS(pixelTableName).Where(x => x.EngineDataID == engineDataID).OrderBy(x => x.CreateTime).ToList();
                    fileIds = pixels.Select(x => x.DrawingFileID).ToList();
                }
                var query = Db.Queryable<ec_drawing_fileEntity>().AS(tableName)
                    .WhereIF(!string.IsNullOrEmpty(DrawingCatalogueID), x => x.DrawingCatalogueID == DrawingCatalogueID)
                    .WhereIF(!string.IsNullOrEmpty(StrDrawingCatalogueIDs), x => SqlFunc.SplitIn(StrDrawingCatalogueIDs, x.DrawingCatalogueID))
                    .WhereIF(!string.IsNullOrEmpty(DrawingFileNameFull), x => x.DrawingFileName == DrawingFileNameFull)
                    .WhereIF(!string.IsNullOrEmpty(DrawingFileIDUnequal), x => x.DrawingFileID != DrawingFileIDUnequal)
                    .WhereIF(IsDelete == "1", x => x.IsDelete == 1)
                    .WhereIF(IsDelete != "1", x => x.IsDelete != 1 || x.IsDelete == null)
                    .WhereIF(!string.IsNullOrEmpty(DrawingFileName), x => x.DrawingFileName.Contains(DrawingFileName))
                    .WhereIF(!string.IsNullOrEmpty(engineDataID), x => fileIds.Contains(x.DrawingFileID));
                List<ec_drawing_fileEntity> list;
                if (pagination == null)
                {
                    list = query.ToList();
                }
                else
                {
                    list = query.ToPageList(pagination.page, pagination.rows);
                }
                if (!string.IsNullOrEmpty(engineDataID))
                {
                    list?.ForEach(x =>
                    {
                        x.PixelCode = string.Join(",", pixels?.Where(y => y.DrawingFileID == x.DrawingFileID).Select(y => y.PixelCode).ToList());
                    });
                }
                var userIds = new List<string>();
                var detailIds = new List<string>();
                list?.ForEach(x =>
                {
                    userIds.Add(x.CreateUserID);
                    userIds.Add(x.CheckOutUserID);
                    userIds.Add(x.UpdateUserID);
                    detailIds.Add(x.DrawingType);
                    detailIds.Add(x.DrawingStage);
                    detailIds.Add(x.DrawingSystem);
                    detailIds.Add(x.MaterialCensus);
                    detailIds.Add(x.MaterialRange);
                });
                var users = Db.Queryable<UserEntity>().Where(x => userIds.Contains(x.F_UserId)).ToList();
                var detailTableName = TableName<ec_dataitemdetailEntity>(ProjectId);
                var details = Db.Queryable<ec_dataitemdetailEntity>().AS(detailTableName).Where(x => detailIds.Contains(x.DataItemDetailID)).ToList();
                list?.ForEach(x =>
                {
                    x.CreateUserName = users.FirstOrDefault(y => y.F_UserId == x.CreateUserID)?.F_RealName;
                    x.CheckOutUserName = users.FirstOrDefault(y => y.F_UserId == x.CheckOutUserID)?.F_RealName;
                    x.UpdateUserName = users.FirstOrDefault(y => y.F_UserId == x.UpdateUserID)?.F_RealName;
                    x.DrawingTypeName = details.FirstOrDefault(y => y.DataItemDetailID == x.DrawingType)?.DataItemName;
                    x.DrawingStageName = details.FirstOrDefault(y => y.DataItemDetailID == x.DrawingStage)?.DataItemName;
                    x.DrawingSystemName = details.FirstOrDefault(y => y.DataItemDetailID == x.DrawingSystem)?.DataItemName;
                    x.MaterialCensusName = details.FirstOrDefault(y => y.DataItemDetailID == x.MaterialCensus)?.DataItemName;
                    x.MaterialRangeName = details.FirstOrDefault(y => y.DataItemDetailID == x.MaterialRange)?.DataItemName;
                });
                return list;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_drawing_fileEntity GetEntity(string keyValue, string ProjectId)
        {
            try
            {
                return ec_drawing_fileService.GetEntity(keyValue, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue, string ProjectId)
        {
            try
            {
                ec_drawing_fileService.DeleteEntity(keyValue, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 逻辑删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void LogicDelete(string keyValue, string ProjectId)
        {
            try
            {
                ec_drawing_fileEntity entityFile = GetEntity(keyValue, ProjectId);
                entityFile.IsDelete = 1;
                entityFile.DeleteTime = Time.MySqlTime;
                UserInfo userInfo = LoginUserInfo.Get();
                entityFile.DeleteUserID = userInfo.userId;
                ec_drawing_fileService.UpdataEntity(entityFile, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        /// <summary>
        /// 还原逻辑删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void RepeatLogicDelete(string keyValue, string ProjectId)
        {
            try
            {
                ec_drawing_fileEntity entityFile = GetEntity(keyValue, ProjectId);
                entityFile.IsDelete = 0;
                entityFile.DeleteTime = null;
                entityFile.DeleteUserID = null;
                ec_drawing_fileService.UpdataEntity(entityFile, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string keyValue, ec_drawing_fileEntity entity, string ProjectId)
        {
            try
            {
                var fileList = new AnnexesFileBLL().GetList(entity.FolderId).ToList();
                if (fileList != null && fileList.Count > 0)
                {
                    if (!string.IsNullOrWhiteSpace(fileList[0].F_FilePath))
                    {
                        entity.FileMD5 = GetMD5HashFromFile(fileList[0].F_FilePath);
                    }
                }
                ec_drawing_fileService.SaveEntity(keyValue, entity, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 获取文件MD5值
        /// </summary>
        /// <param name="file">文件绝对路径</param>
        /// <returns>MD5值</returns>
        public static string GetMD5HashFromFile(string file)
        {
            try
            {
                FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(fileStream);
                fileStream.Close();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                return string.Empty;
                //throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
            }
        }

        /// <summary>
        /// 新增
        /// <summary>
        /// <returns></returns>
        public void InertEntity(ec_drawing_fileEntity entity, string ProjectId)
        {
            try
            {
                ec_drawing_fileService.InertEntity(entity, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 编辑
        /// <summary>
        /// <returns></returns>
        public void EditEntity(ec_drawing_fileEntity entity, string ProjectId)
        {
            try
            {
                ec_drawing_fileService.EditEntity(entity, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 检出
        /// <summary>
        /// <returns></returns>
        public void CheckOutDrawingFile(string drawingFileID, string ProjectId)
        {
            try
            {
                ec_drawing_fileService.CheckOutDrawingFile(drawingFileID, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 检入
        /// <summary>
        /// <returns></returns>
        [Obsolete]
        public void CheckInDrawingFile(string drawingFileID, string remark, string folderId, string fileMD5, string ProjectId, string UsersNotified)
        {
            try
            {
                ec_drawing_fileService.CheckInDrawingFile(drawingFileID, remark, folderId, fileMD5, ProjectId, UsersNotified);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }


        /// <summary>
        /// 检入
        /// <summary>
        /// <returns></returns>
        public void CheckInDrawingFileNew(ec_drawing_synEntity obj, string fileMD5, string ProjectId, string UsersNotified)
        {
            try
            {
                ec_drawing_fileService.CheckInDrawingFileNew(obj,  fileMD5, ProjectId, UsersNotified);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        /// <summary>
        /// 释放
        /// <summary>
        /// <returns></returns>
        public void FreeDrawingFile(string drawingFileID, string ProjectId)
        {
            try
            {
                ec_drawing_fileService.FreeDrawingFile(drawingFileID, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 发布
        /// <summary>
        /// <returns></returns>
        public void PublishDrawingFile(string drawingFileID, string remark, string ProjectId)
        {
            try
            {
                ec_drawing_fileService.PublishDrawingFile(drawingFileID, remark, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 修订
        /// <summary>
        /// <returns></returns>
        public void ModifyDrawingFile(string drawingFileID, string fileVersion, string ProjectId)
        {
            try
            {
                ec_drawing_fileService.ModifyDrawingFile(drawingFileID, fileVersion, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 图纸导入  类似于复制的功能
        /// <param name="drawingCatalogueID">图纸目录ID</param>
        /// <param name="strEntity">包含DrawingFileID和ProjectId</param>
        /// <summary>
        /// <returns></returns>
        public string SaveSelectData(string drawingCatalogueID, string ProjectId, string strEntity)
        {
            string msg = "";
            UserInfo userInfo = LoginUserInfo.Get();
            List<CopyDrawingData> copyDrawingDatas = strEntity.ToList<CopyDrawingData>();

            //复制图纸文件数据到选择图纸目录中
            foreach (var item in copyDrawingDatas)
            {
                //新增的图纸文件数据集合
                List<ec_drawing_fileEntity> addDrawingFileList = new List<ec_drawing_fileEntity>();
                //新增的附件数据集合
                List<AnnexesFileEntity> addAnnexesFileList = new List<AnnexesFileEntity>();
                //新增的工程数据集合
                List<ec_enginedataEntity> addEngineDataList = new List<ec_enginedataEntity>();
                //更新的工程数据集合
                List<ec_enginedataEntity> updateEngineDataList = new List<ec_enginedataEntity>();
                //新增的工程数据图元集合
                List<ec_enginedata_pixelEntity> addPixelList = new List<ec_enginedata_pixelEntity>();
                //新增的工程数据属性集合
                List<ec_enginedata_propertyEntity> addPropertyList = new List<ec_enginedata_propertyEntity>();
                //要删除的属性表数据通过工程数据ID
                List<string> listPropByEngineIds = new List<string>();

                ec_projectEntity ec_projectEntity = new ec_projectService().GetEntity(item.ProjectId);
                var tempDrawingEntity = ec_drawing_fileService.GetEntity(item.DrawingFileID, item.ProjectId);
                //判断图纸名称是否重复
                var listdrawingfile = new ec_drawing_fileBLL().GetList("{DrawingCatalogueID:\"" + drawingCatalogueID + "\",ProjectId:\"" + ProjectId + "\"}").ToList();
                listdrawingfile = listdrawingfile.FindAll(x => x.DrawingFileName == tempDrawingEntity.DrawingFileName);
                if (listdrawingfile != null && listdrawingfile.Count > 0)
                {
                    msg = tempDrawingEntity.DrawingFileName + "，该图纸已经存在，不允许导入！";
                    return msg;
                }
                //创建新的图纸文件ID
                tempDrawingEntity.DrawingFileID = Guid.NewGuid().ToString();
                var fileList = new AnnexesFileBLL().GetList(tempDrawingEntity.FolderId).ToList();
                tempDrawingEntity.FolderId = Guid.NewGuid().ToString();
                //复制图纸文件记录
                for (var i = 0; i < fileList.Count; i++)
                {
                    if (i == 0 && !string.IsNullOrWhiteSpace(fileList[0].F_FilePath))
                    {
                        if (File.Exists(fileList[0].F_FilePath))
                        {
                            tempDrawingEntity.FileMD5 = GetMD5HashFromFile(fileList[0].F_FilePath);
                        }
                    }
                    fileList[i].F_Id = Guid.NewGuid().ToString();
                    fileList[i].F_FolderId = tempDrawingEntity.FolderId;
                    fileList[i].F_CreateDate = Time.MySqlTime;
                    fileList[i].F_CreateUserId = userInfo.userId;
                    fileList[i].F_CreateUserName = userInfo.realName;
                    addAnnexesFileList.Add(fileList[i]);
                }
                tempDrawingEntity.DrawingCatalogueID = drawingCatalogueID;
                tempDrawingEntity.CreateTime = Time.MySqlTime;
                tempDrawingEntity.CreateUserID = userInfo.userId;
                tempDrawingEntity.UpdateTime = null;
                tempDrawingEntity.UpdateUserID = null;
                addDrawingFileList.Add(tempDrawingEntity);

                //获取拷贝图纸文件对应的工程数据图元数据
                var tempPixelList = ec_enginedata_pixelService.GetList("{ProjectId:\"" + item.ProjectId + "\",DrawingFileID:\"" + item.DrawingFileID + "\"}").ToList();
                if (tempPixelList != null && tempPixelList.Count() > 0)
                {
                    //获取拷贝图纸所有的工程数据ID
                    List<string> listEngineDataId = tempPixelList.Select(x => x.EngineDataID).Distinct().ToList();
                    foreach (var engineDataId in listEngineDataId)
                    {
                        //工程数据ID
                        string strEngineDataID = string.Empty;

                        //获取拷贝图纸工程数据实体
                        var tempEngineDataEntity = ec_enginedataService.GetEntity(engineDataId, item.ProjectId);
                        if (tempEngineDataEntity == null)
                        {
                            continue;
                        }
                        //创建新的工程数据ID
                        tempEngineDataEntity.EngineDataID = Guid.NewGuid().ToString();
                        //复制工程数据。位号 = 原来位号 + from + 原项目名称
                        tempEngineDataEntity.TagNumber = tempEngineDataEntity.TagNumber + "_from_" + ec_projectEntity.ProjectName;
                        string curObjectTypeID = string.Empty;
                        //对象类型ID根据对象类型库的完整路径(中文)判断。
                        List<ec_objecttypeEntity> copyObjectTypeData = new ec_objecttypeBLL().GetList("{ProjectId:\"" + item.ProjectId + "\",ObjectTypeID:\"" + tempEngineDataEntity.ObjectTypeID + "\"}").ToList();
                        if (copyObjectTypeData != null && copyObjectTypeData.Count > 0)
                        {
                            List<ec_objecttypeEntity> curObjectTypeData = new ec_objecttypeBLL().GetList("{ProjectId:\"" + ProjectId + "\",FullPathCN:\"" + copyObjectTypeData[0].FullPathCN + "\"}").ToList();
                            if (curObjectTypeData != null && curObjectTypeData.Count > 0)
                            {
                                curObjectTypeID = curObjectTypeData[0].ObjectTypeID;
                            }

                        }
                        if (string.IsNullOrEmpty(curObjectTypeID))
                        {
                            msg = tempEngineDataEntity.TagNumber + "对象类型(" + copyObjectTypeData[0].FullPathCN + ")不匹配！";
                            return msg;
                        }
                        tempEngineDataEntity.ObjectTypeID = curObjectTypeID;

                        //判断当前项目中工程数据是否存在
                        List<ec_enginedataEntity> curdEngineData = new ec_enginedataBLL().GetList("{ProjectId:\"" + ProjectId + "\",TagNumber:\"" + tempEngineDataEntity.TagNumber + "\"}").ToList();
                        if (curdEngineData != null && curdEngineData.Count > 0)
                        {
                            strEngineDataID = curdEngineData[0].EngineDataID;

                            //删除当前项目的属性数据
                            listPropByEngineIds.Add(curdEngineData[0].EngineDataID);

                            curdEngineData[0].ObjectTypeID = curObjectTypeID;
                            curdEngineData[0].UpdateTime = Time.MySqlTime;
                            curdEngineData[0].UpdateUserID = userInfo.userId;
                            //更新工程数据表
                            updateEngineDataList.Add(curdEngineData[0]);

                        }
                        else
                        {
                            strEngineDataID = tempEngineDataEntity.EngineDataID;

                            //根据被复制的对象类型ID获取完整路径(中文)对应的对象类型ID
                            tempEngineDataEntity.CreateTime = Time.MySqlTime;
                            tempEngineDataEntity.CreateUserID = userInfo.userId;
                            tempEngineDataEntity.UpdateTime = null;
                            tempEngineDataEntity.UpdateUserID = null;
                            tempEngineDataEntity.DataStatus = "00002";
                            //插入工程数据表
                            addEngineDataList.Add(tempEngineDataEntity);

                        }

                        //获取工程数据对应的图元数据
                        var tempPixel = tempPixelList.FindAll(x => x.EngineDataID == engineDataId);
                        if (tempPixel != null && tempPixel.Count > 0)
                        {
                            foreach (var pixel in tempPixel)
                            {
                                pixel.EngineDataPixelID = Guid.NewGuid().ToString();
                                pixel.EngineDataID = strEngineDataID;
                                pixel.DrawingFileID = tempDrawingEntity.DrawingFileID;
                                pixel.CreateTime = Time.MySqlTime;
                                pixel.CreateUserID = userInfo.userId;
                                pixel.UpdateTime = null;
                                pixel.UpdateUserID = null;
                                addPixelList.Add(pixel);
                            }
                        }

                        //获取对应的工程数据属性
                        var tempPropertyList = new ec_enginedata_propertyService().GetTagPropById(item.ProjectId, engineDataId).ToList();
                        if (tempPropertyList != null && tempPropertyList.Count > 0)
                        {
                            foreach (var property in tempPropertyList)
                            {
                                property.EngineDataPropertyID = Guid.NewGuid().ToString();
                                property.EngineDataID = strEngineDataID;

                                addPropertyList.Add(property);
                            }
                        }

                    }
                }
                ec_drawing_fileService.SaveSelectData(addDrawingFileList,
                                    addAnnexesFileList,
                                    addEngineDataList,
                                    updateEngineDataList,
                                    addPixelList,
                                    addPropertyList,
                                    listPropByEngineIds,
                                    ProjectId);
            }

            return msg;
        }

        /// <summary>
        /// 前端传过来的图纸导入实体
        /// </summary>
        public class CopyDrawingData
        {
            /// <summary>
            /// 图纸文件ID
            /// </summary>
            /// <returns></returns>
            public string DrawingFileID { get; set; }
            /// <summary>
            /// 项目ID
            /// </summary>
            /// <returns></returns>
            public string ProjectId { get; set; }
        }

        /// <summary>
        /// 修改集合数据,新增附件数据集合
        /// <summary>
        /// <returns></returns>
        public void SaveDataAndAnnexes(string ProjectId)
        {
            try
            {
                UserInfo userInfo = LoginUserInfo.Get();
                List<AnnexesFileEntity> listAnnexesFile = new List<AnnexesFileEntity>();
                var drawingFileData = new ec_drawing_fileBLL().GetList("{ProjectId:\"" + ProjectId + "\"}").ToList();
                foreach (var item in drawingFileData)
                {
                    var fileList = new AnnexesFileService().GetList(item.FolderId).ToList();
                    item.FolderId = Guid.NewGuid().ToString();
                    for (var i = 0; i < fileList.Count; i++)
                    {
                        if (i == 0 && !string.IsNullOrWhiteSpace(fileList[0].F_FilePath))
                        {
                            if (System.IO.File.Exists(fileList[0].F_FilePath))
                            {
                                item.FileMD5 = GetMD5HashFromFile(fileList[0].F_FilePath);
                            }
                        }
                        fileList[i].F_Id = Guid.NewGuid().ToString();
                        fileList[i].F_FolderId = item.FolderId;
                        fileList[i].F_CreateDate = Time.MySqlTime;
                        fileList[i].F_CreateUserId = userInfo.userId;
                        fileList[i].F_CreateUserName = userInfo.realName;
                        listAnnexesFile.Add(fileList[i]);
                    }
                }
                ec_drawing_fileService.SaveDataAndAnnexes(drawingFileData, listAnnexesFile, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        #endregion
    }
}
